package frontera;

import javax.swing.*;

public class Login extends JPanel {

    private VentanaPrincipal ventana;

    public void setMensaje(String mensaje) {
        mensajeError.setText(mensaje);
    }

    public Login(VentanaPrincipal v) {
        initComponents();
        mensajeError.setVisible(false);
        ventana= v;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelLogin = new javax.swing.JPanel();
        usuarioTF = new javax.swing.JTextField();
        iniciarB = new javax.swing.JButton();
        mensajeError = new javax.swing.JLabel();
        contrasenaTF = new javax.swing.JPasswordField();

        panelLogin.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        panelLogin.setPreferredSize(new java.awt.Dimension(200, 170));
        panelLogin.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                panelLoginKeyReleased(evt);
            }
        });

        usuarioTF.setFont(new java.awt.Font("Segoe UI Light", 0, 18)); // NOI18N
        usuarioTF.setForeground(new java.awt.Color(153, 153, 153));
        usuarioTF.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        usuarioTF.setText("Usuario");
        usuarioTF.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                usuarioTFMouseClicked(evt);
            }
        });
        usuarioTF.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                usuarioTFFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                usuarioTFFocusLost(evt);
            }
        });
        usuarioTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                usuarioTFKeyPressed(evt);
            }
        });

        iniciarB.setText("Iniciar Sesión");
        iniciarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                iniciarBActionPerformed(evt);
            }
        });

        mensajeError.setFont(new java.awt.Font("Segoe UI", 0, 10)); // NOI18N
        mensajeError.setForeground(new java.awt.Color(255, 0, 0));
        mensajeError.setText("Compruebe su usuario o contraseña");

        contrasenaTF.setFont(new java.awt.Font("Segoe UI Light", 0, 18)); // NOI18N
        contrasenaTF.setForeground(new java.awt.Color(153, 153, 153));
        contrasenaTF.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        contrasenaTF.setText("Contraseña");
        contrasenaTF.setPreferredSize(new java.awt.Dimension(92, 31));
        contrasenaTF.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                contrasenaTFMouseClicked(evt);
            }
        });
        contrasenaTF.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                contrasenaTFFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                contrasenaTFFocusLost(evt);
            }
        });
        contrasenaTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                contrasenaTFKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout panelLoginLayout = new javax.swing.GroupLayout(panelLogin);
        panelLogin.setLayout(panelLoginLayout);
        panelLoginLayout.setHorizontalGroup(
            panelLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelLoginLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(usuarioTF)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelLoginLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(iniciarB))
                    .addComponent(mensajeError, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                    .addComponent(contrasenaTF, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        panelLoginLayout.setVerticalGroup(
            panelLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelLoginLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(usuarioTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(contrasenaTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(mensajeError)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                .addComponent(iniciarB)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(99, Short.MAX_VALUE)
                .addComponent(panelLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(101, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(65, Short.MAX_VALUE)
                .addComponent(panelLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(65, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void iniciarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_iniciarBActionPerformed
        if (!iniciarSesion(usuarioTF, contrasenaTF)) {
            mensajeError.setVisible(true);
        }
    }//GEN-LAST:event_iniciarBActionPerformed

    private void usuarioTFFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_usuarioTFFocusGained
        if (!usuarioTF.getText().equals("Usuario")) {
            enfocado(usuarioTF, "", new java.awt.Color(255,255,255));
        }
    }//GEN-LAST:event_usuarioTFFocusGained

    private void usuarioTFKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_usuarioTFKeyPressed
        usuarioTF.setForeground(new java.awt.Color(255,255,255));
        panelLoginKeyReleased(evt);
    }//GEN-LAST:event_usuarioTFKeyPressed

    private void usuarioTFFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_usuarioTFFocusLost
        if (usuarioTF.getText().equals("Usuario") || (usuarioTF.getText().replace(" ", "").equals(""))) {
            enfocado(usuarioTF, "Usuario", new java.awt.Color(153,153,153));
        }
    }//GEN-LAST:event_usuarioTFFocusLost

    private void usuarioTFMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_usuarioTFMouseClicked
        enfocado(usuarioTF, "", new java.awt.Color(255,255,255));
    }//GEN-LAST:event_usuarioTFMouseClicked

    private void panelLoginKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_panelLoginKeyReleased
        int key = evt.getKeyCode();
        if (key == java.awt.event.KeyEvent.VK_ENTER) {
            if (!iniciarSesion(usuarioTF, contrasenaTF)) {
                mensajeError.setVisible(true);
            }
        } else if (key == java.awt.event.KeyEvent.VK_TAB) {
            enfocado(contrasenaTF, "", new java.awt.Color(255,255,255));
        } else if (key == java.awt.event.KeyEvent.VK_TAB) {
            enfocado(usuarioTF, "", new java.awt.Color(255,255,255));
        }
    }//GEN-LAST:event_panelLoginKeyReleased

    private void contrasenaTFFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_contrasenaTFFocusGained
        if (!String.copyValueOf(contrasenaTF.getPassword()).equals("Contraseña")) {
            enfocado(contrasenaTF, "", new java.awt.Color(255,255,255));
        }
    }//GEN-LAST:event_contrasenaTFFocusGained

    private void contrasenaTFKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_contrasenaTFKeyPressed
        contrasenaTF.setForeground(new java.awt.Color(255,255,255));
        //borrarContraseña(evt);
        panelLoginKeyReleased(evt);
    }//GEN-LAST:event_contrasenaTFKeyPressed

    private void contrasenaTFFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_contrasenaTFFocusLost
        if (String.copyValueOf(contrasenaTF.getPassword()).equals("Contraseña") ||
            (String.copyValueOf(contrasenaTF.getPassword()).replace(" ", "").equals(""))) {
            enfocado(contrasenaTF, "Contraseña", new java.awt.Color(153,153,153));
        }
    }//GEN-LAST:event_contrasenaTFFocusLost

    private void contrasenaTFMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_contrasenaTFMouseClicked
        enfocado(contrasenaTF, "", new java.awt.Color(255,255,255));
    }//GEN-LAST:event_contrasenaTFMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPasswordField contrasenaTF;
    private javax.swing.JButton iniciarB;
    private javax.swing.JLabel mensajeError;
    private javax.swing.JPanel panelLogin;
    private javax.swing.JTextField usuarioTF;
    // End of variables declaration//GEN-END:variables

    private void enfocado(JTextField obj, String t, java.awt.Color s) {
        obj.setText(t);
        obj.setForeground(s);
    }

    private boolean iniciarSesion(JTextField usuarioTF, JPasswordField contrasenaTF) {        
        String muestre = VentanaPrincipal.controlUsuario.validarLogin(usuarioTF.getText(),
        String.copyValueOf(contrasenaTF.getPassword()));
        
        if (!muestre.equals("Bienvenido")){
            mensajeError.setText(muestre);
            return false;
        }
        ventana.iniciarPanelPrincipal();
        return true;
        
    }
    
}

